package com.store.bookstore_backend.service.impl;

import com.store.bookstore_backend.domain.Book;
import com.store.bookstore_backend.domain.Collect;
import com.store.bookstore_backend.dto.CollectGetDTO;
import com.store.bookstore_backend.mapper.BookMapper;
import com.store.bookstore_backend.mapper.CollectMapper;
import com.store.bookstore_backend.service.CollectService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    BookMapper bookMapper;
    @Override
    public List<CollectGetDTO> getCollectList(HttpServletRequest request) {
        Long userId =(Long)request.getAttribute("userId");
        List<CollectGetDTO> collectGetDTOList = new ArrayList<>();
        List<Collect>collectList = collectMapper.selectByUserId(userId);
        for (Collect collect : collectList) {
            CollectGetDTO collectGetDTO = new CollectGetDTO();
            Book book = bookMapper.selectBookById(collect.getBookId());
            collectGetDTO.setBookId(book.getId());
            collectGetDTO.setId(collect.getId());
            collectGetDTO.setName(book.getName());
            collectGetDTO.setCover(book.getCover());
            collectGetDTO.setPrice(book.getPrice());
            collectGetDTOList.add(collectGetDTO);
        }
        return collectGetDTOList;
    }

    @Override
    public void insertCollect(Integer bookId,HttpServletRequest request) {
        Collect collect = new Collect();
        collect.setBookId(bookId);
        collect.setUserId((Long)request.getAttribute("userId"));
        collectMapper.insert(collect);
    }

    @Override
    public void DeleteCollect(List<Integer> Ids) {
        for (Integer Id : Ids) {
            collectMapper.delete(Id);
        }
    }

}
